package com.talent.controller;

import com.talent.common.PageResult;
import com.talent.common.Result;
import com.talent.pojo.dto.NoticeDTO;
import com.talent.pojo.entity.Notice;
import com.talent.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 通知公告控制器
 */
@RestController
@RequestMapping("/api/notices")
@Slf4j
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 分页查询通知
     */
    @GetMapping
    public Result<PageResult<Notice>> pageQuery(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer pageSize,
                                                String title) {
        log.info("分页查询通知：page={}, pageSize={}, title={}", page, pageSize, title);
        PageResult<Notice> pageResult = noticeService.pageQuery(page, pageSize, title);
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询通知
     */
    @GetMapping("/{id}")
    public Result<Notice> getById(@PathVariable Long id) {
        log.info("根据ID查询通知：{}", id);
        Notice notice = noticeService.getById(id);
        return Result.success(notice);
    }

    /**
     * 新增通知
     */
    @PostMapping
    public Result<String> save(@Valid @RequestBody NoticeDTO noticeDTO) {
        log.info("新增通知：{}", noticeDTO);
        noticeService.save(noticeDTO);
        return Result.success("新增成功");
    }

    /**
     * 更新通知
     */
    @PutMapping("/{id}")
    public Result<String> update(@PathVariable Long id, @Valid @RequestBody NoticeDTO noticeDTO) {
        log.info("更新通知：id={}, data={}", id, noticeDTO);
        noticeService.update(id, noticeDTO);
        return Result.success("更新成功");
    }

    /**
     * 删除通知
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Long id) {
        log.info("删除通知：{}", id);
        noticeService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 批量删除通知
     */
    @DeleteMapping
    public Result<String> deleteByIds(@RequestParam Long[] ids) {
        log.info("批量删除通知：{}", ids);
        noticeService.deleteByIds(ids);
        return Result.success("删除成功");
    }
}